package oop.main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.text.MaskFormatter;

/**
 * 
 *  This class will be used to display the Frame to submit a new student
 *  It is also the class that is used to create a student object
 * 
 * @author Zach Schulz
 * 
 */
public class Student extends Person
{

	public int id;
	public String advisor;

	private double gpa;

	public static double MAX_GPA = 4.0;
	public static double MIN_GPA = 0.1;

	private static JTextField txtFirstName;
	private static JTextField txtLastName;
	private static JTextField txtAge;
	private static JFormattedTextField txtSsn;
	private static JTextField txtId;
	private static JFormattedTextField txtGpa;
	private static JTextField txtAdvisor;
	private static JButton btnSubmit;
	private static JButton btnCancel;
	private static JFrame frame;

	public static void main(String[] args)
	{

		frame = new JFrame("Student");

		MaskFormatter ssnFormat = null;
		try
		{
			ssnFormat = new MaskFormatter("###-##-####");
		} catch (ParseException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		MaskFormatter gpaFormat = null;
		try
		{
			gpaFormat = new MaskFormatter("#.#");
		} catch (ParseException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		txtFirstName = new JTextField();
		txtLastName = new JTextField();
		txtSsn = new JFormattedTextField(ssnFormat);
		txtAge = new JTextField();
		txtId = new JTextField();
		txtGpa = new JFormattedTextField(gpaFormat);
		txtAdvisor = new JTextField();

		btnSubmit = new JButton("Submit");
		btnCancel = new JButton("Cancel");

		frame.setLayout(new GridLayout(8, 2));
		frame.setSize(500, 500);

		Border txtBorder = BorderFactory.createEtchedBorder();

		frame.add(new JLabel("First Name: "));
		txtFirstName.setBorder(txtBorder);
		frame.add(txtFirstName);

		frame.add(new JLabel("Last Name: "));
		txtLastName.setBorder(txtBorder);
		frame.add(txtLastName);

		frame.add(new JLabel("Age: "));
		txtAge.setBorder(txtBorder);
		frame.add(txtAge);

		frame.add(new JLabel("SSN: "));
		txtSsn.setBorder(txtBorder);
		frame.add(txtSsn);

		frame.add(new JLabel("Student ID: "));
		txtId.setBorder(txtBorder);
		frame.add(txtId);

		frame.add(new JLabel("GPA: "));
		txtGpa.setBorder(txtBorder);
		frame.add(txtGpa);

		frame.add(new JLabel("Advisor: "));
		txtAdvisor.setBorder(txtBorder);
		frame.add(txtAdvisor);

		btnSubmit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				submitBtnClicked(e);
			}
		});
		frame.add(btnSubmit);

		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				cancelBtnClicked(e);
			}
		});
		frame.add(btnCancel);

		frame.setVisible(true);

	}

	public Student(String firstName, String lastName, int age, String ssn, int id,
			String advisor, double gpa)
	{
		super(firstName, lastName, age, ssn);

		this.id = id;
		this.advisor = advisor;
		setGpa(gpa);
	}


	/**
	 * @return the gpa
	 */
	public double getGpa()
	{
		return gpa;
	}

	/**
	 * @param gpa
	 *            the gpa to set
	 */
	public void setGpa(double gpa)
	{
		if(gpa > 4 || gpa < 0)
		{
			System.out.println("Invalid GPA");
		}else
		{
			this.gpa = gpa;
		}
	}

	private static void submitBtnClicked(ActionEvent e)
	{
		boolean isValid = true;
		
		final String SSN = "\\d{3}-\\d{2}-\\d{4}";
		final String GPA = "\\d.\\d";
		
		if(txtFirstName.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(frame, "The First Name you entered is not valid! \n You entered: " + txtFirstName.getText());
			isValid = false;
		}
		if(txtLastName.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(frame, "The Last Name you entered is not valid! \n You entered: " + txtLastName.getText());
			isValid = false;
		}
		if(!isInteger(txtAge.getText()))
		{
			JOptionPane.showMessageDialog(frame, "The Age you entered is not valid! \n You entered: " + txtAge.getText());
			isValid = false;
		}
		if(!txtSsn.getText().matches(SSN))
		{
			JOptionPane.showMessageDialog(frame, "The SSN you entered is not valid! \n You entered: " + txtSsn.getText());
			isValid = false;
		}
		if(!isInteger(txtId.getText()))
		{
			JOptionPane.showMessageDialog(frame, "The Student ID you entered is not valid! \n You entered: " + txtId.getText());
			isValid = false;
		}
		if(!txtGpa.getText().matches(GPA))
		{
			JOptionPane.showMessageDialog(frame, "The GPA you entered is not valid! \n You entered: " + txtGpa.getText());
			isValid = false;
		}
		if(txtAdvisor.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(frame, "The Advisor you entered is not valid! \n You entered: " + txtAdvisor.getText());
			isValid = false;
		}
		if(isValid)
		{
			Student myStudent = new Student(txtFirstName.getText(),
					txtLastName.getText(), Integer.parseInt(txtAge.getText()),
					txtSsn.getText(), Integer.parseInt(txtId
							.getText()), txtAdvisor.getText(),
					Double.parseDouble(txtGpa.getText()));
			
			myStudent.toString();
		}
		
	}

	private static void cancelBtnClicked(ActionEvent e)
	{
		txtAdvisor.setText("");
		txtAge.setText("");
		txtFirstName.setText("");
		txtLastName.setText("");
		txtId.setText("");
		txtSsn.setText("");

		frame.setVisible(false);
		
	}

	@Override
	public String toString()
	{
		String studentString;
		studentString = "Student Id: " + id + "\nFirst Name: " + firstName
				+ "\nLast Name: " + lastName + "\nSSN:" + getSsn() + "\nAge: "
				+ age + "\nGPA: " + getGpa() + "\nAdvisor: " + advisor;
		System.out.println(studentString);
		return studentString;
	}
	
	private static boolean isInteger(String x)
	{
		try  
		  {  
		    int d = Integer.parseInt(x);  
		  }  
		  catch(NumberFormatException nfe)  
		  {  
		    return false;  
		  }  
		  return true; 
	}
}
